package com.example.asus.a222dzd_ordering.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.example.asus.a222dzd_ordering.R;
import com.example.asus.a222dzd_ordering.adapter.FoodListAdapter;
import com.example.asus.a222dzd_ordering.bean.FoodListBean;


import java.util.List;

public class SearchResultActivity extends AppCompatActivity{
    private RecyclerView recyclerView;
    private List<FoodListBean> beans;
    private FoodListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_result);
        Intent getIntent = getIntent();
        beans = (List<FoodListBean>) getIntent.getSerializableExtra("beanlist");
        initView();
        initData();
        initEvent();
    }
    private void initView(){
        adapter = new FoodListAdapter();
        recyclerView = findViewById(R.id.rv_searchresult);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
    }

    private void initData(){
        adapter.setData(beans);
    }

    private void initEvent(){
        adapter.setOnItemClickListener(new FoodListAdapter.OnItemClickListener() {
            @Override
            public void setItem(View view, int position) {
                FoodListBean bean = beans.get(position);
                Intent intent = new Intent(getApplicationContext(),FoodContentActivity.class);
                intent.putExtra("foodname",bean.getFoodname());
                intent.putExtra("foodprice",bean.getPrice());
                intent.putExtra("foodpic",bean.getPic());
                intent.putExtra("foodid",bean.getFood_id());
                startActivity(intent);
            }
        });
    }
}
